/**
 * @author: 杨钧凯
 * @date：2023-06-19 09:54
 */
import scala.Int;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;

public class sqlout {
    public static void main(String[] args) {
        String jdbcUrl = "jdbc:mysql://localhost:3306/stuInfo";
        String username = "root";
        String password = "865831578";

        try {
            // 连接数据库
            Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
            Statement statement = connection.createStatement();

            // 执行查询
            String sql = "SELECT * FROM student";
            ResultSet resultSet = statement.executeQuery(sql);

            // 创建txt文件
            FileWriter fileWriter = new FileWriter("D:\\codes\\untitled2\\output.txt");

            // 将查询结果写入txt文件
            while (resultSet.next()) {
                Integer classname = resultSet.getInt("classNu");
                //String Str = resultSet.getString("Str");
                //Integer sex = resultSet.getInt("sexx");
                //Integer birth = resultSet.getInt("birth");
                Integer stuId = resultSet.getInt("stuId");
                //Integer semeter = resultSet.getInt("semeter");
                Integer jidian = resultSet.getInt("jidian");
                //String origin = resultSet.getString("origin");
                fileWriter.write(  classname+ "\t" + stuId + "\t" + jidian + "\n");
            }

            // 关闭资源
            fileWriter.close();
            resultSet.close();
            statement.close();
            connection.close();

            System.out.println("数据提取完成并保存到output.txt文件。");

        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }
}

